<template>
    <div class="add">
        <van-field
            v-model="title"
            label="问题标题"
            placeholder="请输入问题名称"
        />

        <van-field
            v-model="content"
            rows="1"
            autosize
            label="问题详细描述"
            type="textarea"
            placeholder="请输入问题描述"
        />

        <van-dropdown-menu>
            <van-dropdown-item v-model="tid" :options="typeList" />
        </van-dropdown-menu>

        <van-button
            @click="onSave"
            block
            type="primary
        "
            >添加</van-button
        >
    </div>
</template>

<script>
import axios from "axios";
export default {
    async created() {
        const res = await axios.get("/questions/types");
        this.typeList = res.data.data.map((item) => ({
            text: item.name,
            value: item.id,
        }));
    },
    data() {
        return {
            title: "",
            content: "",
            tid: 1,
            typeList: [],
        };
    },
    methods: {
        async onSave() {
            const { title, content, tid } = this;
            console.log({ title, content, tid });
            const res = await axios.post("/questions", { title, content, tid });
            if (res.data.code !== 201) {
                // ..
                return;
            }
            this.$router.push("/main/home");
        },
    },
};
</script>

<style lang="less" scoped></style>
